package swarm // Package swarm import "xemo.top/docker/docker/api/types/swarm"
import (
	"time"
	"xemo.top/docker/docker/api/types/swarm/runtime"
)

// TaskState represents the state of a task.
type TaskState string

const (
	// TaskStateNew NEW
	TaskStateNew TaskState = "new"
	// TaskStateAllocated ALLOCATED
	TaskStateAllocated TaskState = "allocated"
	// TaskStatePending PENDING
	TaskStatePending TaskState = "pending"
	// TaskStateAssigned ASSIGNED
	TaskStateAssigned TaskState = "accepted"
	// TaskStatePreparing PREPARING
	TaskStatePreparing TaskState = "preparing"
	// TaskStateReady READY
	TaskStateReady TaskState = "ready"
	// TaskStateStarting STARTING
	TaskStateStarting TaskState = "starting"
	// TaskStateRunning RUNNING
	TaskStateRunning TaskState = "running"
	// TaskStateComplete COMPLETE
	TaskStateComplete TaskState = "complete"
	// TaskStateShutdown SHUTDOWN
	TaskStateShutdown TaskState = "shutdown"
	// TaskStateFailed FAILED
	TaskStateFailed TaskState = "failed"
	// TaskStateRejected REJECTED
	TaskStateRejected TaskState = "rejected"
	// TaskStateRemove REMOVE
	TaskStateRemove TaskState = "remove"
	// TaskStateOrphaned ORPHANED
	TaskStateOrphaned TaskState = "orphaned"
)

// Resources represents resources (CPU/Memory)
type Resources struct {
	NanoCPUs         int64             `json:",omitempty"`
	MemoryBytes      int64             `json:",omitempty"`
	GenericResources []GenericResource `json:",omitempty"`
}

// GenericResource 表示 “用户定义” 资源，可以是整数 (例如: SSD = 3) 或字符串 (例如: SSD = sda1)
// GenericResource represents a "user defined" resource which can be either an integer (e.g: SSD=3) or a string (e.g: SSD=sda1)
type GenericResource struct {
	NamedResourceSpec    *NamedGenericResource    `json:",omitempty"`
	DiscreteResourceSpec *DiscreteGenericResource `json:",omitempty"`
}

// DiscreteGenericResource 表示被定义为整数的 “使用定义的” 资源 “种类” 用于描述资源的种类 (例如: “GPU”，“FPGA”，“SSD”，...)值用于计算资源 (SSD = 5，HDD = 3，..)
// DiscreteGenericResource represents a "used defined" resource which is defined as an integer "Kind" is used to describe the Kind of a resource (e.g: "GPU","FPGA","SSD",...) Value is used to count the resource (SSD=5,HDD=3,..)
type DiscreteGenericResource struct {
	Kind  string `json:",omitempty"`
	Value int64  `json:",omitempty"`
}

// NamedGenericResource 表示定义为字符串的 “用户定义” 资源。“种类” 用于描述资源的种类 (例如: “GPU”，“FPGA”，“SSD”，...)值用于标识资源 (GPU = "UUID-1"，FPGA = "/dev/sdb5"，...)
// NamedGenericResource represents a "user defined" resource which is defined as a string. "Kind" is used to describe the Kind of a resource (e.g: "GPU","FPGA","SSD",...) Value is used to identify the resource (GPU="UUID-1",FPGA="/dev/sdb5",...)
type NamedGenericResource struct {
	Kind  string `json:",omitempty"`
	Value string `json:",omitempty"`
}

// TaskSpec represents the spec of a task
type TaskSpec struct {
	// ContainerSpec、NetworkAttachmentSpec和PluginSpec是互斥的。仅当 “runtime” 字段设置为 “plugin” 时才使用PluginSpec。如果 “runtime” 字段设置为 “attachment”，则使用NetworkAttachmentSpec。
	// ContainerSpec,NetworkAttachmentSpec,and PluginSpec are mutually exclusive. PluginSpec is only used when the `Runtime` field is set to `plugin` NetworkAttachmentSpec is used if the `Runtime` field is set to `attachment`.
	ContainerSpec         *ContainerSpec            `json:",omitempty"`
	PluginSpec            *runtime.PluginSpec       `json:",omitempty"`
	NetworkAttachmentSpec *NetworkAttachmentSpec    `json:",omitempty"`
	Resources             *ResourceRequirements     `json:",omitempty"`
	RestartPolicy         *RestartPolicy            `json:",omitempty"`
	Placement             *Placement                `json:",omitempty"`
	Networks              []NetworkAttachmentConfig `json:",omitempty"`

	// LogDriver 指定用于从此规范创建的任务的日志驱动程序。如果不存在，则将使用swarm.spec上的集群默认值，如果未指定，则最终回退到引擎默认值。
	// LogDriver specifies the LogDriver to use for tasks created from this spec.If not present,the one on cluster default on swarm.Spec will be used,finally falling back to the engine default if not specified.
	LogDriver *Driver `json:",omitempty"`
	// ForceUpdate 是一个计数器，即使没有更改相关参数，也会触发更新。
	// ForceUpdate is a counter that triggers an update even if no relevant parameters have been changed.
	ForceUpdate uint64
	Runtime     RuntimeType `json:",omitempty"`
}

// Placement 表示业务流程参数
// Placement represents orchestration parameters
type Placement struct {
	Constraints []string              `json:",omitempty"`
	Preferences []PlacementPreference `json:",omitempty"`
	MaxReplicas uint64                `json:",omitempty"`
	// Platforms 存储映像可以在其上运行的所有平台。此字段在平台筛选器中用于调度。如果为空，则平台筛选器处于关闭状态，这意味着没有调度限制
	// Platforms stores all the platforms that the images can run on.This field is used in the platform filter for scheduling.If empty,then the platform filter is off,meaning there are no scheduling restrictions
	Platforms []Platform `json:",omitempty"`
}

// PlacementPreference 提供一种使调度程序了解拓扑等因素的方法。
// PlacementPreference provides a way to make the scheduler aware of factors such as topology.
type PlacementPreference struct {
	Spread *SpreadOver
}

// SpreadOver 是一个调度首选项，指示调度程序将任务均匀地分布在由标签标识的节点组上。
// SpreadOver is a scheduling preference that instructs the scheduler to spread tasks evenly over groups of nodes identified by labels.
type SpreadOver struct {
	// 标签描述符，如engine.labels.az
	// label descriptor , such as engine.labels.az
	SpreadDescriptor string
}

// RestartPolicy represents the restart policy.
type RestartPolicy struct {
	Condition   RestartPolicyCondition `json:",omitempty"`
	Delay       *time.Duration         `json:",omitempty"`
	MaxAttempts *uint64                `json:",omitempty"`
	Window      *time.Duration         `json:",omitempty"`
}

// RestartPolicyCondition represents when to restart
type RestartPolicyCondition string

const (
	// RestartPolicyConditionNone NONE
	RestartPolicyConditionNone RestartPolicyCondition = "none"
	// RestartPolicyConditionOnFailure ON_FAILURE
	RestartPolicyConditionOnFailure RestartPolicyCondition = "on-failure"
	// RestartPolicyConditionAny ANY
	RestartPolicyConditionAny RestartPolicyCondition = "any"
)

// ResourceRequirements represents resources requirements
type ResourceRequirements struct {
	Limits       *Resources `json:",omitempty"`
	Reservations *Resources `json:",omitempty"`
}

// Task represents a task
type Task struct {
	ID string
	Meta
	Annotations
	Spec                TaskSpec            `json:",omitempty"`
	ServiceID           string              `json:",omitempty"`
	Slot                int                 `json:",omitempty"`
	NodeID              string              `json:",omitempty"`
	Status              TaskStatus          `json:",omitempty"`
	DesiredState        TaskState           `json:",omitempty"`
	NetworksAttachments []NetworkAttachment `json:",omitempty"`
	GenericResources    []GenericResource   `json:",omitempty"`
}

// TaskStatus represents the status of a task.
type TaskStatus struct {
	Timestamp time.Time `json:",omitempty"`
	State     TaskState `json:",omitempty"`
}
